package com.binfenhulian.chinaenergy.ces.manage.core.url2io.processor;

import com.binfenhulian.chinaenergy.ces.core.exception.BaseException;
import com.binfenhulian.chinaenergy.ces.manage.core.url2io.processor.download.DownloadImagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 图片转存服务
 * Created by wanghjbuf on 2018/3/19.
 */
@Slf4j
@Component("com.binfenhulian.chinaenergy.ces.manage.core.url2io.processor.DownPicExtraProcessor")
public class DownPicExtraProcessor implements BaseExtraProcessor{

    @Resource(name = "com.binfenhulian.chinaenergy.ces.manage.core.url2io.processor.download.DownloadImagService")
    DownloadImagService downloadImagService;

    @Override
    public String processTitle(String title) throws BaseException {
        return title;
    }

    @Override
    public String processContent(String content) throws BaseException {
        String Regex="src=\".*?(jpg|gif|jpeg|png)\"";

        Pattern p=Pattern.compile(Regex);
        Matcher matcher=p.matcher(content);
        while (matcher.find()) {
            String src = matcher.group();
            String imageUrl = src.substring(4, src.length());
            log.info("imageUrl:{};", imageUrl);

            content = content.replaceAll(imageUrl, downloadImagService.transform(imageUrl));
        }
        return content;
    }

    @Override
    public String processDate(String date) throws BaseException {
        return date;
    }
}
